// const baseUrl = 'http://api.sxxat.cn/App/Xsk/';
// const baseUrlApi = "http://182.92.204.237:5003/api/"
const baseUrlApi = "https://cardapi.sxxat.cn/api/"
const baseUrl = 'https://api.sxxat.cn/App/Xsk/';
// const baseUrl = 'http://ikceym9.nat.ipyingshe.com/App/Xsk/';
// 设备
const sbBaseUrl = 'https://cardapi.sxxat.cn/api/';	
// const sbBaseUrl = 'http://182.92.204.237:5003/api/';
const httpRequest = (opts, data) => {
	let httpDefaultOpts = {
		url: baseUrl + opts.url,
		data: data,
		method: 'post',
		header: 'post' == 'get' ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};
const httpDeveiceRequest = (opts, data) => {
	let httpDefaultOpts = {
		url: sbBaseUrl + opts.url,
		data: data,
		method: 'post',
		header: 'post' == 'get' ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "*/*",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/json; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};
// get请求
const httpGetRequest = (opts, data) => {
	let httpDefaultOpts = {
		url: baseUrl + opts.url,
		data: data,
		method: 'get',
		header: 'post' == 'get' ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};
//带Token请求
const httpTokenRequest = (opts, data) => {
	let token = "",
		userId = "";
	uni.getStorage({
		key: 'token',
		success: function(ress) {
			token = ress.data
		}
	});
	// uni.getStorage({
	// 	key: 'userInfo',
	// 	success: function(ress) {
	// 		userId = ress.data.user_id
	// 	}
	// });
	//此token是登录成功后后台返回保存在storage中的
	let httpDefaultOpts = {
		url: baseUrl + opts.url,
		data: data,
		method: 'post',
		header: 'post' == 'get' ? {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'token': token,
			// "userId":userId,
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};
//带Token请求
const taskhttpTokenRequest = (opts, data) => {
	let token = "",
		userId = "";
	uni.getStorage({
		key: 'token',
		success: function(ress) {
			token = ress.data
		}
	});
	//此token是登录成功后后台返回保存在storage中的
	let httpDefaultOpts = {
		url: baseUrl + opts.url,
		data: data.data,
		method: 'post',
		header: 'post' == 'get' ? {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"bjId":data.bjId,
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'token': token,
			"bjId":data.bjId,
			// "userId":userId,
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};
//带Token请求
const httpDeviceTokenRequest = (opts, data) => {
	let token = "",
		userId = "";
	uni.getStorage({
		key: 'token',
		success: function(ress) {
			token = ress.data
		}
	});
	// uni.getStorage({
	// 	key: 'userInfo',
	// 	success: function(ress) {
	// 		userId = ress.data.user_id
	// 	}
	// });
	//此token是登录成功后后台返回保存在storage中的
	let httpDefaultOpts = {
		url: sbBaseUrl + opts.url,
		data: data,
		method: 'post',
		header: 'post' == 'get' ? {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'token': token,
			// "userId":userId,
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};

// * 小程序支付调用
// * wePay(provider, timeStamp, nonceStr, packages, signType, paySign,res=>{},fail=>{})

const wePay = (provider, timeStamp, nonceStr, packages, signType, paySign, success, fail) => (
	uni.requestPayment({
		provider,
		timeStamp,
		nonceStr,
		package: packages,
		signType,
		paySign,
		success(res) {
			success(JSON.stringify(res));
		},
		fail(err) {
			fail(JSON.stringify(err))
		}
	})
)


export default {
	baseUrl,
	httpRequest,
	httpDeveiceRequest,
	httpDeviceTokenRequest,
	httpTokenRequest,
	httpGetRequest,
	wePay,
	taskhttpTokenRequest
}
